﻿using _2._16._2015_Blog.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace _2._16._2015_Blog
{
    public sealed class BlogDatabase
    {
        #region FirstRegion

        private static BlogDatabase _instance = null;
        private int _currId;

        private BlogDatabase()
        {
            _blogPosts = new List<BlogPost>();

            _blogPosts.Add(new BlogPost {Title = "Title1", Text = "blah", UserName = "V"});
        }

        public static BlogDatabase Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new BlogDatabase();
                }
                return _instance;               
            }
        }
        #endregion

        private List<BlogPost> _blogPosts;

        #region CRUD Methods

        public BlogListViewModel GetBlogListViewModel()
        {
            return new BlogListViewModel
            {
                Blogs = _blogPosts.Select(b => new BlogViewModel
                {
                    Id = b.Id,
                    DateCreated = b.DateCreated,
                    Comments = b.Comments.Select(c => new CommentViewModel
                    {
                        BlogPostId = b.Id,
                        DateCreated = c.DateCreated,
                        Text = c.Text,
                        UserName = c.UserName
                    }).ToList(),
                    Text = b.Text,
                    Title = b.Title,
                    UserName = b.UserName
                }).ToList(),
                NumberOfBlogs = _blogPosts.Count
            };
        }

        public BlogViewModel GetBlogViewModel(int id)
        {
            return _blogPosts.Where(b => b.Id == id).Select(b => new BlogViewModel
            {
                Id = b.Id,
                DateCreated = b.DateCreated,
                Comments = b.Comments.Select(c => new CommentViewModel
                {
                    BlogPostId = b.Id,
                    DateCreated = c.DateCreated,
                    Text = c.Text,
                    UserName = c.UserName
                }).ToList(),
                Text = b.Text,
                Title = b.Title,
                UserName = b.UserName,
                NewComment = new CreateCommentViewModel { BlogPostId = b.Id }
            }).FirstOrDefault();
        }

        public void CreateBlogPost(CreateBlogViewModel model)
        {
            _blogPosts.Add(new BlogPost
            {
                Id = ++_currId,
                Text = model.Text,
                Title = model.Title,
                UserName = model.UserName
            });
        }

        public BlogViewModel CreateComment(CreateCommentViewModel model)
        {
            BlogPost bp = _blogPosts.FirstOrDefault(b => b.Id == model.BlogPostId);
            bp.Comments.Add(new Comment
            {
                BlogPostId = model.BlogPostId,
                Text = model.Text,
                UserName = model.UserName
            });

            return GetBlogViewModel(model.BlogPostId);
        }

        #endregion
    }
}